$(document).ready(function() {
   $("#ordertable-modify").hide();
   $(".confirm-button1").click(function (e) {
        e.preventDefault();
        $row = document.getElementById("modifyTable");
        var data = [];
        for (var i = 0, rows = $row.rows.length; i < rows; i++) {
            for (var j = 0, cells = $row.rows[i].cells.length; j < cells; j++) {
                if (!data[i]) {
                    data[i] = [];
                }

                data[i][j] = $row.rows[i].cells[j].innerHTML;
            }
        }
        console.log(data[1][8])
        console.log(data)
        $.ajax({
            type: 'PUT',
            url: '/user/modifyResult',
            data:{
                "bid":data[1][0],
                "bname":data[1][1],
                "category1":data[1][2],
                "category2":data[1][3],
                "description":data[1][4],
                "isLoaned":data[1][5],
                "times":data[1][6],
                "content":data[1][7],
                "price":data[1][8],
                "score":data[1][9],
                "author":data[1][10],
                "pubUnit":data[1][11]
            },
            dataType: "json",
            success: function (data) {
                $('tbody tr').remove();

                $("#ordertable-modify").hide();

                $.each(data, function (index, item) {
                    console.log(item.bid);
                    let $row = $('<tr>');
                    $('<td>').text(item.bid).appendTo($row);
                    $('<td>').text(item.bname).appendTo($row);
                    $('<td>').text(item.category1).appendTo($row);
                    $('<td>').text(item.category2).appendTo($row);
                    $('<td>').text(item.description).appendTo($row);
                    $('<td>').text(item.isLoaned).appendTo($row);
                    $('<td>').text(item.times).appendTo($row);
                    $('<td>').text(item.content).appendTo($row);

                    let $image = $('<td>')
                    $('<img>').attr('src',item.cover).attr('width','150px').attr('height','150px').appendTo($image).appendTo($row);

                    $('<td>').text(item.price).appendTo($row);
                    $('<td>').text(item.score).appendTo($row);
                    $('<td>').text(item.author).appendTo($row);
                    $('<td>').text(item.pubUnit).appendTo($row);

                    let $cell = $('<td>');
                    let $btn_modify = $('<button>'),
                        $btn_delete = $('<button>');

                    $btn_modify.addClass("button-container");

                    $btn_modify.attr('id',item.bid)


                    $btn_delete.addClass("button-delete-book");
                    $btn_delete.attr('name',item.bid)

                    $btn_modify.text('编辑').appendTo($cell);
                    $btn_delete.text('删除').appendTo($cell);

                    $cell.appendTo($row);

                    $row.appendTo('#ordertable tbody');
                    });

                }
            })
        });
   $(document).on('click','.button-container', function (e) {
    console.log("我点击了修改按钮哦！")
        var id = $(this).attr('id')
        console.log(id)
        e.preventDefault();
        $.ajax({
            type: 'post',
            url: '/user/modify',
            data: {"id":id},
            dataType: "json",
            success: function (data) {
            console.log(data)
                    let $row = $('<tr>');
                    let $zero = $('<td>'),
                        $first = $('<td>'),
                        $second = $('<td>'),
                        $third = $('<td>'),
                        $forth = $('<td>'),
                        $fifth = $('<td>'),
                        $sixth = $('<td>'),
                        $seventh = $('<td>'),
                        $eighth = $('<td>'),
                        $ninth = $('<td>'),
                        $tenth = $('<td>'),
                        $eleventh = $('<td>'),
                        $twelfth = $('<td>');

                    $zero.text(data[0].bid).appendTo($row);
                    $zero.addClass("remake");
                    $first.text(data[0].bname).appendTo($row);
                    $first.addClass("remake");
                    $second.text(data[0].category1).appendTo($row);
                    $second.addClass("remake");
                    $third.text(data[0].category2).appendTo($row);
                    $third.addClass("remake");
                    $forth.text(data[0].description).appendTo($row);
                    $forth.addClass("remake");
                    $fifth.text(data[0].isLoaned).appendTo($row);
                    $fifth.addClass("remake");
                    $sixth.text(data[0].times).appendTo($row);
                    $sixth.addClass("remake");
                    $seventh.text(data[0].content).appendTo($row);
                    $seventh.addClass("remake");

                    let $image = $('<td>')
                    $('<img>').attr('src',data[0].cover).attr('width','80px').attr('height','80px').appendTo($image).appendTo($row);

                    $ninth.text(data[0].price).appendTo($row);
                    $ninth.addClass("remake");
                    $tenth.text(data[0].score).appendTo($row);
                    $tenth.addClass("remake");
                    $eleventh.text(data[0].author).appendTo($row);
                    $eleventh.addClass("remake");
                    $twelfth.text(data[0].pubUnit).appendTo($row);
                    $twelfth.addClass("remake");

                    $row.appendTo('#ordertable-modify tbody');


                    var remake = document.getElementsByClassName("remake");

                    for (let i = 0; i < remake.length; i++) {

                        remake[i].onclick = () => {
                            remake[i].style.display = "none";
                            var para = document.createElement("input");
                            var ctd = document.createElement("td");
                            para.value = remake[i].innerHTML;
                            remake[i].parentNode.insertBefore(ctd, remake[i]);
                            ctd.appendChild(para, ctd);
                            para.focus();
                            para.onblur = () => {
                                remake[i].innerHTML = para.value;
                                remake[i].parentNode.removeChild(ctd);
                                remake[i].style.display = ""
                            }
                        }
                    }


            }
        });
        $("#ordertable-modify").show();
    });
   $(document).on('click','.button-delete-book', function (e) {
        console.log("我点击了删除按钮！")
        var name = $(this).attr('name')
        console.log(name)
        e.preventDefault();
        $.ajax({
            type: 'post',
            url: '/user/delete',
            data: {"name":name},
            dataType: "json",
            success: function (data) {
            $('tbody tr').remove();
                $.each(data, function (index, item) {
                    console.log(item.bid);
                    let $row = $('<tr>');
                    $('<td>').text(item.bid).appendTo($row);
                    $('<td>').text(item.bname).appendTo($row);
                    $('<td>').text(item.category1).appendTo($row);
                    $('<td>').text(item.category2).appendTo($row);
                    $('<td>').text(item.description).appendTo($row);
                    $('<td>').text(item.isLoaned).appendTo($row);
                    $('<td>').text(item.times).appendTo($row);
                    $('<td>').text(item.content).appendTo($row);

                    let $image = $('<td>')
                    $('<img>').attr('src',item.cover).attr('width','150px').attr('height','150px').appendTo($image).appendTo($row);

                    $('<td>').text(item.price).appendTo($row);
                    $('<td>').text(item.score).appendTo($row);
                    $('<td>').text(item.author).appendTo($row);
                    $('<td>').text(item.pubUnit).appendTo($row);

                    let $cell = $('<td>');
                    let $btn_modify = $('<button>'),
                        $btn_delete = $('<button>');

                    $btn_modify.addClass("button-container");

                    $btn_modify.attr('id',item.bid)

                    $btn_delete.addClass("button-delete-book");
                    $btn_delete.attr('name',item.bid)

                    $btn_modify.text('编辑').appendTo($cell);
                    $btn_delete.text('删除').appendTo($cell);

                    $cell.appendTo($row);

                    $row.appendTo('#ordertable tbody');
                    });
            }
        });
        $("#ordertable-modify").hide();
    });
   $(document).on('click','.cancel-button-books', function (e) {
        console.log("我点击了返回按钮！")
        e.preventDefault();
        $.ajax({
            type: 'post',
            url: '/user/returnBook',
            dataType: "json",
            success: function (data) {
            $('tbody tr').remove();
                $.each(data, function (index, item) {
                    console.log(item.bid);
                    let $row = $('<tr>');
                    $('<td>').text(item.bid).appendTo($row);
                    $('<td>').text(item.bname).appendTo($row);
                    $('<td>').text(item.category1).appendTo($row);
                    $('<td>').text(item.category2).appendTo($row);
                    $('<td>').text(item.description).appendTo($row);
                    $('<td>').text(item.isLoaned).appendTo($row);
                    $('<td>').text(item.times).appendTo($row);
                    $('<td>').text(item.content).appendTo($row);

                    let $image = $('<td>')
                    $('<img>').attr('src',item.cover).attr('width','150px').attr('height','150px').appendTo($image).appendTo($row);

                    $('<td>').text(item.price).appendTo($row);
                    $('<td>').text(item.score).appendTo($row);
                    $('<td>').text(item.author).appendTo($row);
                    $('<td>').text(item.pubUnit).appendTo($row);

                    let $cell = $('<td>');
                    let $btn_modify = $('<button>'),
                        $btn_delete = $('<button>');

                    $btn_modify.addClass("button-container");

                    $btn_modify.attr('id',item.bid)

                    $btn_delete.addClass("button-delete-book");
                    $btn_delete.attr('name',item.bid)

                    $btn_modify.text('编辑').appendTo($cell);
                    $btn_delete.text('删除').appendTo($cell);

                    $cell.appendTo($row);

                    $row.appendTo('#ordertable tbody');
                    });
            }
        });
        $("#ordertable-modify").hide();
    });
});

